using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Ink;
using System.Windows;
using System.Xml.Serialization;

using WozLib.UI.Ink;
using System.Diagnostics;
using WozLib.UI.Ink.SketchObjects;

namespace WozLib.UI.Ink.SketchCommands
{
  public class ModifyUserToolsEnabledCollection : RememberSketchCommand
  {
    [XmlElement("NewTool")]
    public List<SketchPad.InkTools> NewTools
    {
      get { return _NewTools; }
      set { _NewTools = value; }
    } private List<SketchPad.InkTools> _NewTools = new List<SketchPad.InkTools>();

    [XmlElement("PreviousTool")]
    public List<SketchPad.InkTools> PreviousTools
    {
      get { return _PreviousTools; }
      set { _PreviousTools = value; }
    } private List<SketchPad.InkTools> _PreviousTools = new List<SketchPad.InkTools>();

    [XmlElement("RememberCommand")]
    public bool RememberCommand
    {
      get { return _RememberCommand; }
      set { _RememberCommand = value; }
    } private bool _RememberCommand;

    public ModifyUserToolsEnabledCollection()
    {
    }

    public ModifyUserToolsEnabledCollection(List<SketchPad.InkTools> newTools, List<SketchPad.InkTools> previousTools)
      : base()
    {
      NewTools = newTools;
      PreviousTools = previousTools;
    }

    public ModifyUserToolsEnabledCollection(List<SketchPad.InkTools> newTools)
      : base()
    {
      NewTools = newTools;
      // todo keep track that we need to save and load this ourself
      RememberCommand = true;
      RegisterObject();
    }

    public override void Redo()
    {
      try
      {
        if (this.WozClientCommandReferences.InkCanvas != null)
        {
          if (RememberCommand)
          {
            RegisterObject();
            PreviousTools = this.WozClientCommandReferences.InkCanvas.UserToolsEnabledCollection;
          }

          this.WozClientCommandReferences.InkCanvas.UserToolsEnabledCollection = NewTools;
        }
      }
      catch (Exception ex)
      {
        Debug.WriteLine(ex.ToString());
      }
    }

    public override void Undo()
    {
      try
      {
        if (RememberCommand)
        {
          RememberSketchCommand wcrc = GetCommand(WozClientClearCommandID);
          if (wcrc != null && wcrc is ModifyUserToolsEnabledCollection)
          {
            ModifyUserToolsEnabledCollection modifyUserCurrentInkTool = wcrc as ModifyUserToolsEnabledCollection;
            if (modifyUserCurrentInkTool.WozClientCommandReferences != null)
            {
              if (modifyUserCurrentInkTool.WozClientCommandReferences.InkCanvas != null)
              {
                modifyUserCurrentInkTool.WozClientCommandReferences.InkCanvas.UserToolsEnabledCollection = modifyUserCurrentInkTool.PreviousTools;
              }
            }
          }
        }
        else
        {
          if (this.WozClientCommandReferences.InkCanvas != null)
          {
            this.WozClientCommandReferences.InkCanvas.UserToolsEnabledCollection = PreviousTools;
          }
        }
      }
      catch (Exception ex)
      {
        Debug.WriteLine(ex.ToString());
      }
    }

  }
}
